package cn.matezk.lianlian.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.matezk.lianlian.model.SecretKey;
import cn.matezk.lianlian.service.ISecretKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api")
public class SecretController {
    @Autowired
    private ISecretKeyService secretKeyService;

    @GetMapping("appKeySelector")
    public List<JSONObject> selector() {
        List<SecretKey> list = secretKeyService.selectAll();
        if (ObjectUtil.isEmpty(list))   return new ArrayList<>();
        List<JSONObject> ret = new ArrayList<>();
        list.forEach(item->ret.add(JSONUtil.createObj()
                .set("label", String.format("%s(%s)", item.getCustomer(), item.getAppKey()))
                .set("value", item.getAppKey())
        ));
        return ret;
    }
}
